package sn.hn.demo;

import sn.hn.demo.image.ImageData;
import sn.hn.demo.thread.ShopLoop;
import sn.hn.demo.unit.Mermaid;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ShopView extends SurfaceView implements SurfaceHolder.Callback{
	public ShopLoop mThread;
	public Bitmap mBK;
	public Bitmap mSo,mItem1,mItem2,mItem3,mItem4,mPlay,mPlayCLick,mOvuong;
	public Paint mPaint;
	public Mermaid mMermaid;
	public long mTime;
	public boolean isClickPlay = false;
	public int itemChoose = 0;
	public ShopView(Context context) {
		super(context);
		init();
	}
	
	public void reset(){
		isClickPlay = false;
	}
	private void init()
	{
		reset();
		getHolder().addCallback(this);
		mThread = new ShopLoop(getHolder(), this);
		mPlay = BitmapFactory.decodeResource(getResources(), R.drawable.play);
		mPlayCLick = BitmapFactory.decodeResource(getResources(), R.drawable.playclick);
		mOvuong = ImageData.mSquare;
		mBK = ImageData.mBackgroundShop;
		mSo = ImageData.mSo;
		mItem1 = ImageData.mItem1;
		mItem2 = ImageData.mItem2;
		mItem3 = ImageData.mItem3;
		mItem4 = ImageData.mItem4;
		mPaint = new Paint();
		mPaint.setFilterBitmap(true);
		mMermaid = new Mermaid(ShopActivity.mContext);
	}
	public float xText = 3*MainActivity.SCREENWIDTH/8;
	public float yText = 7*MainActivity.SCREENHEIGHT/8;
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Rect rect = new Rect(0, 0, getWidth(), getHeight());
		canvas.drawBitmap(mBK, null, rect, mPaint);
		mMermaid.mSprite.loop(mTime);
		mMermaid.mSprite.draw(canvas);
		canvas.drawBitmap(mSo, (float)13*MainActivity.SCREENWIDTH/40, MainActivity.SCREENHEIGHT/2, mPaint);
		canvas.drawBitmap(mItem1, (float)59.5*MainActivity.SCREENWIDTH/160,(float)6.8*MainActivity.SCREENHEIGHT/12, mPaint);
		canvas.drawBitmap(mSo, (float)MainActivity.SCREENWIDTH/2, MainActivity.SCREENHEIGHT/2, mPaint);
		canvas.drawBitmap(mItem2, (float)43.5*MainActivity.SCREENWIDTH/80,(float)8.9*MainActivity.SCREENHEIGHT/16, mPaint);
		canvas.drawBitmap(mSo, (float)53*MainActivity.SCREENWIDTH/80, MainActivity.SCREENHEIGHT/2, mPaint);
		canvas.drawBitmap(mItem3, (float)11*MainActivity.SCREENWIDTH/16,(float)1.9*MainActivity.SCREENHEIGHT/3, mPaint);
		canvas.drawBitmap(mSo, (float)33*MainActivity.SCREENWIDTH/40, MainActivity.SCREENHEIGHT/2, mPaint);
		canvas.drawBitmap(mItem4, (float)70*MainActivity.SCREENWIDTH/80,(float)6.7*MainActivity.SCREENHEIGHT/12, mPaint);
		switch (itemChoose) {
		case 0:
			canvas.drawText("Co ba la...", xText, yText, mPaint);
			canvas.drawBitmap(mOvuong, (float)27.5*MainActivity.SCREENWIDTH/80,(float)17*MainActivity.SCREENHEIGHT/32, mPaint);
			break;
		case 1:
			canvas.drawText("Giun...", xText, yText, mPaint);
			canvas.drawBitmap(mOvuong, (float)41.4*MainActivity.SCREENWIDTH/80,(float)17*MainActivity.SCREENHEIGHT/32, mPaint);
			break;
		case 2:
			canvas.drawText("Day thung...", xText, yText, mPaint);
			canvas.drawBitmap(mOvuong, (float)55*MainActivity.SCREENWIDTH/80,(float)17*MainActivity.SCREENHEIGHT/32, mPaint);
			break;
		case 3:
			canvas.drawText("Dong ho cat...", xText, yText, mPaint);
			canvas.drawBitmap(mOvuong, (float)68*MainActivity.SCREENWIDTH/80,(float)17*MainActivity.SCREENHEIGHT/32, mPaint);
			break;
		default:
			break;
		}
		if(isClickPlay)
			canvas.drawBitmap(mPlay,xPlay,yPlay, mPaint);
		else
			canvas.drawBitmap(mPlayCLick, xPlay,yPlay, mPaint);
	}
	public float xPlay = (float)12.8*MainActivity.SCREENWIDTH/16;
	public float yPlay = (float)5.25*MainActivity.SCREENHEIGHT/24;
	
	public float xItem1 = (float)59.5*MainActivity.SCREENWIDTH/160;
	public float yItem1 = (float)6.8*MainActivity.SCREENHEIGHT/12;
	
	public float xItem2 = (float)43.5*MainActivity.SCREENWIDTH/80;
	public float yItem2 = (float)8.9*MainActivity.SCREENHEIGHT/16;
	
	public float xItem3 = (float)11*MainActivity.SCREENWIDTH/16;
	public float yItem3 = (float)1.9*MainActivity.SCREENHEIGHT/3;
	
	public float xItem4 = (float)70*MainActivity.SCREENWIDTH/80;
	public float yItem4 = (float)6.7*MainActivity.SCREENHEIGHT/12;
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if(mThread.getState() == Thread.State.TERMINATED)
		{
			mThread = new ShopLoop(getHolder(), this);
		}
		mThread.setRunning(true);
		try {
			mThread.start();
		} catch (IllegalThreadStateException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		mThread.setRunning(false);
		while (retry)
		{
			try
			{
				mThread.join();
				retry = false;
			}
			catch (InterruptedException e){}
		}
	}
}
